package cn.zniot.so.application.util;

import cn.zniot.so.transport.exception.ZniotError;

import org.apache.mina.util.Transform;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * 工具类
 */
public class MethodUtil {

	/**
	 * 打印异常信息
	 */
	public static String printExceptionErr(Throwable cause) {
		StringBuffer errSb = new StringBuffer(64);

		String[] ee = Transform.getThrowableStrRep(cause);
		errSb.append("Sending package Exception.\r\n");
		for (int i = 0; i < ee.length; i++) {
			errSb.append(ee[i]);
			errSb.append("\r\n");
		}
		return errSb.toString();
	}

	/**
	 * 把字节数组转换为16进制字符串
	 * 
	 * @param data : -127-128 范围内的byte类型ASCII值 例如发送过来0011
	 *            0001(2进制)==49(10进制)==31(16进制)==1(实际要传的字符)
	 */
	public static String Hex2String(byte[] data) {
		StringBuffer sb = new StringBuffer(50);
		for (int i = 0, j = data.length; i < j; i++) {
			int value = data[i] & 0x000000ff;// & 0x000000FF的作用是:如果data[i]是负数，则会清除前面24个零，正的byte整型不受影响。
			String stemp = Integer.toHexString(value);// 表示成16进制字符串
			if (stemp.length() < 2) {
				sb.append("0");
			}
			sb.append(stemp);
			sb.append(" ");
		}
		// sb.append("\r\n");// 添加回车(\r)换行(\n)字符 
		return sb.toString();
	}

	/**
	 * 计算经度
	 * 
	 * @param : 如11618.130740
	 * @return : 结果116.302179
	 */
	public static String calculateLongitude(String srcString) {
		String temp = "";
		if (null == srcString || "".equals(srcString)) {
			return temp;
		}
		temp = String.valueOf(Integer.valueOf(srcString.substring(0, 3))
				+ new DecimalFormat("#.000000").format(Double.valueOf(srcString.substring(3))/ (60)));
				
		return temp;
	}

	public static void main(String[] args) {
		System.out.println(MethodUtil.calculateLongitude("11618.58294"));// 116.30971566666666
		System.out.println(new DecimalFormat("#.000000").format(Double
				.valueOf(MethodUtil.calculateLongitude("11618.58294"))));
		System.out.println(MethodUtil.calculateLatitude("4003.22823"));//40.05380383333333
	}

	/**
	 * 计算纬度
	 * 
	 * @param :
	 *            如4002.146980
	 * @return : 结果40.035783
	 */
	public static String calculateLatitude(String srcString) {
		String temp = "";
		if (null == srcString || "".equals(srcString)) {
			return temp;
		}
		temp = String.valueOf(Integer.valueOf(srcString.substring(0, 2))
				 + new DecimalFormat("#.000000").format(Double.valueOf(srcString.substring(2)) / 60));
		return temp;
	}

	/**
	 * 取得字符串格式指定时间
	 * 
	 * @param date
	 * @param format
	 * @return
	 */
	public static String getDateTime(String format) {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.format(cal.getTime());
	}

	/**
	 * @param temp
	 * @return
	 */
	public static String toHexStringDropSpace(byte[] data) {
		StringBuffer sb = new StringBuffer(50);
		for (int i = 0, j = data.length; i < j; i++) {
			String value = Integer.toHexString(data[i] & 0xff);
			if (value.length() < 2) {
				sb.append("0");
			}
			sb.append(Integer.toHexString(data[i] & 0xff));
		}
		return sb.toString();
	}

	public static boolean stringIsEmpty(String str){
		if(str == null || str.trim().length() == 0){
			return true;
		}else return false;
	}
	
	public static boolean stringIsNotEmpty(String str){
		if (str != null && str.trim().length() > 0) {
			return true;
		} else
			return false;
	}
	
	/**
	 * 获取错误描述
	 */
	public static String getExceptionCauseMsg(Throwable e, int causeType) {
		String error = null;
		if (causeType == ZniotError.Zniot_DB_POOL) {
			error = ZniotError.Zniot_DB_OPEN_STR;
		} else if (causeType == ZniotError.Zniot_DB_OPEN) {
			error = ZniotError.Zniot_DB_OPEN_STR;
		} else if (causeType == ZniotError.Zniot_DB_DESTORY) {
			error = ZniotError.Zniot_DB_DESTORY_STR;
		} else if (causeType == ZniotError.Zniot_PAGE_WRITE) {
			error = ZniotError.Zniot_PAGE_WRITE_STR;
		} else if (causeType == ZniotError.Zniot_PARAMETER) {
			error = ZniotError.Zniot_PARAMETER_STR;
		} else if (causeType == ZniotError.Zniot_FAILURE) {
			error = ZniotError.Zniot_FAILURE_STR;
		} else if (causeType == ZniotError.Zniot_AUTHENTICATION) {
			error = ZniotError.Zniot_AUTHENTICATION_STR;
		} else if (causeType == ZniotError.Zniot_NOCONTENT) {
			error = ZniotError.Zniot_NOCONTENT_STR;
		} else {
			error = e.getMessage();
		}
		return error;
	}
}
